package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {
    @Autowired
    SpecParamService specParamService;

    @Override
    public List<SpecGroupDTO> queryGroupAndDetailByCategories(Long id) {
        List<SpecGroupDTO> specGroupDTOS = SpecGroupDTO.convertEntityList(this.query().eq("category_id", id).list());
        if(!CollectionUtils.isEmpty(specGroupDTOS)){
            specGroupDTOS.forEach(specGroupDTO -> {
                List<SpecParam> params = specParamService.query().eq("group_id", specGroupDTO.getId()).list();
                specGroupDTO.setParams(SpecParamDTO.convertEntityList(params));
            });
            return specGroupDTOS;
        }else {
            throw new LyException(404,"没有此规格组");
        }
    }
}